//
//  HJBaseController.m
//  KaiChat
//
//  Created by lx on 2023/10/16.
//

#import "HJBaseController.h"
#import "HJBackgroundView.h"

@interface HJBaseController ()

@property (nonatomic, strong) HJBackgroundView *backgroundView;

@end

@implementation HJBaseController

 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self -> _statusBarFrameHeight = UIApplication.sharedApplication.statusBarFrame.size.height;
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;

    self.view.backgroundColor = UIColor.tj_backgroundColor;
    
    _topBackgroundImage = [UIImage imageNamed:@"login_top_bg"];

    if (!self.hiddenNavigationBar) {
        
        if (!_navigationBar.superview) {
            [self.view addSubview:self.navigationBar];
        }
        if (self.navigationController.viewControllers.count > 1) {
            self.navigationBar.title = self.title;
            [self addBackButton];
        } else {
            if (self.modalPresentationStyle == UIModalPresentationFullScreen) {
                [self addBackButton];
            }
        }
    }
    
    if (!self.hiddenBackgroundView && !_backgroundView.superview) {
        self.navigationBar.alpha = 0;
        self.backgroundView.image = self.topBackgroundImage;
        [self.view addSubview:self.backgroundView];
    } else {
        self.navigationBar.alpha = 1;
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
 
    if (_navigationBar) {
        self.navigationBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.statusBarFrameHeight + 44);
        [self.view bringSubviewToFront:self.navigationBar];
    }
    
    self.backgroundView.frame = self.view.bounds;
}

- (HJNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[HJNavigationBar alloc] init];
        _navigationBar.titleTextColor = UIColor.blackColor;
        _navigationBar.lineView.hidden = YES;
        if (self.navigationController.childViewControllers.count > 1) {
            _navigationBar.title = self.title;
        }
        _navigationBar.backgroundColor = UIColor.whiteColor;
    }
    return _navigationBar;
}

- (HJBackgroundView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[HJBackgroundView alloc] init];
        _backgroundView.backgroundColor = UIColor.tj_backgroundColor;
    }
    return _backgroundView;
}

- (void)popViewController {
    
    if (!self.navigationController) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    if (self.navigationController.viewControllers.count == 1) {
        if(self.presentingViewController) {
            [self dismissViewControllerAnimated:YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)setHiddenNavigationBar:(BOOL)hiddenNavigationBar {
    _hiddenNavigationBar = hiddenNavigationBar;
    if (hiddenNavigationBar) {
        [_navigationBar removeFromSuperview];
    }
}

- (void)setHiddenBackButton:(BOOL)hiddenBackButton {
    _hiddenBackButton = hiddenBackButton;
    self.navigationBar.hiddenBackButton = hiddenBackButton;
}

- (void)addBackButton {
    self.navigationBar.leftBarButton = [HJBackButton barButtonWithImage:[UIImage imageNamed:[self backButtonImageName]] target:self action:@selector(popViewController)];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}

- (BOOL)hiddenBackgroundView {
    return YES;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSString *)backButtonImageName {
    return @"nav_back_default";
}

@end
